package com.qd.panda.space;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cdqidi.constant.ConstantDto;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.util.page.BaseQuery;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.panda.domain.entity.space.OwnerMyIncomeSpaceDTO;
import com.qd.common.panda.domain.entity.space.OwnerParkSpaceDTO;
import com.qd.common.panda.domain.entity.space.ParkSpaceDTO;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.space.ParkSpaceHolderService;
import com.qd.panda.service.space.ParkSpaceService;
import com.qd.panda.service.space.StateUtilService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;

/**
 * 短期租赁/APP_业主端车位租赁信息
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/ownParkSpace/")
@Slf4j
public class OwnParkSpaceController {
    private final ParkSpaceService parkSpaceService;
    private final ParkSpaceHolderService parkSpaceHolderService;

    /**
     * 查询预期收益
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @GetMapping(value = "/expectedRevenue")
    public ApiResultWrapper<Object> expectedRevenue(ParkSpaceDTO dto) {
        if (!StringUtils.hasLength(dto.getLeaseType())) {
            return ApiResult.error("租赁类型不能为空");
        }
        if (!StringUtils.hasLength(dto.getAid())) {
            return ApiResult.error("车位不能为空");
        }
        if (!StringUtils.hasLength(dto.getStartDate())) {
            return ApiResult.error("租赁开始时间不能为空");
        }
        if (!StringUtils.hasLength(dto.getEndDate())) {
            return ApiResult.error("租赁结束时间不能为空");
        }
        return ApiResult.success(parkSpaceService.expectedRevenue(dto));
    }

    /**
     * 添加
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/save")
    public ParkSpaceDTO save(@Validated({DefaultGroup.class}) @RequestBody ParkSpaceDTO dto) {
        dto.setSid(IdWorker.getIdStr());
        dto.setState(StateUtilService.STATE_1);
        dto.setPublishTime(LocalDateTime.now());
        dto.setIsPublish(ConstantDto.SF1);
        dto.setPublisher(SecurityUtils.getLoginUser().getUserId());
        dto.setRentable(ConstantDto.SF1);
        return parkSpaceService.save(dto);
    }

    /**
     * 停止
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PutMapping(value = "/stop")
    public ApiResultWrapper<Object> stop(@Validated @RequestBody ParkSpaceDTO dto) {
        if (!StringUtils.hasLength(dto.getSid())) {
            return ApiResult.error("主键不能为空");
        }
        dto.setState(StateUtilService.STATE_2);
        dto.setEnd(ConstantDto.SF1);
        dto.setRentable(ConstantDto.SF0);
        parkSpaceService.stop(dto);
        return ApiResult.success("停止成功");
    }

    /**
     * 获得最后一次发布
     *
     * @return 发布信息
     */
    @GetMapping(value = "/getLast")
    public OwnerParkSpaceDTO getLast() {
        LocalDate now = LocalDate.now();
        OwnerParkSpaceDTO last = parkSpaceService.getBaseMapper().getLast(SecurityUtils.getLoginUser().getUserId(), null, now, now);
        Optional.ofNullable(last).ifPresent(parkSpaceService::buildDisplay);
        return last;
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<OwnerParkSpaceDTO> page(ParkSpaceDTO dto) {
        dto = Optional.ofNullable(dto).orElse(new ParkSpaceDTO());
        return parkSpaceService.ownerPage(PageUtil.buildPage(dto));
    }

    /**
     * 收益合计
     * @return 合计信息
     */
    @GetMapping(value = "/ownParkAmount")
    public ApiResultWrapper<Object> ownParkAmount(){
        return ApiResult.success(parkSpaceService.ownParkAmount(SecurityUtils.getLoginUser().getUserId()));
    }

    /**
     * 收益列表
     *
     * @return 合计信息
     */
    @GetMapping(value = "/ownParkAmountList")
    public IPage<OwnerMyIncomeSpaceDTO> ownParkAmountList(BaseQuery dto) {
        dto = Optional.ofNullable(dto).orElse(new BaseQuery());
        return parkSpaceService.ownMyIncome(PageUtil.buildPage(dto));
    }

    /**
     * 收益详情
     */
    @GetMapping(value = "/ownParkAmountDetailBySid")
    public ApiResultWrapper<Object> ownParkAmountDetailBySid(@Validated @NotBlank(message = "租赁ID不能为空") String sid) {
        return ApiResult.success(parkSpaceHolderService.ownParkAmountDetailBySid(sid));
    }
}